package com.wu.service.impl;

import com.wu.mapper.CommentReportMapper;
import com.wu.mapper.CommentsMapper;
import com.wu.pojo.Comments;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.wu.constant.Constant.COMMENT_ID;
import static com.wu.constant.Constant.REPORT_ID;

/**
 * @program: blog
 * @author: 吴磊
 * @create: 2021-05-23 15:36
 */
@Service
public class ScheduledService {

    @Autowired
    private CommentsMapper commentsMapper;

    @Autowired
    private CommentReportMapper commentReportMapper;

    /**
     * 定时删除无用的评论举报
     */
//    @Scheduled(cron = "0 9 16 * * ?")
    @Scheduled(cron = "0 0 0 * * ?")
    @Async
    public void deleteCommentReport() {
        List<Map<String, String>> mapList = commentReportMapper.getAllCommentReportId();
        mapList.forEach(map -> {
            String report_id = map.get(REPORT_ID);
            String comment_id = map.get(COMMENT_ID);
            Comments comments = commentsMapper.getCommentsByCommentId(comment_id);
            if (comments == null) {
                commentReportMapper.deleteCommentReportById(report_id);
            }
        });
    }
}